﻿using LitJson;
using LNet;
using OestsCommon.Model;
using OestsDataBase;
using OestsServer.Net;
using System.Collections.Generic;
using System.Linq;

namespace OestsServer.Handlers.OESPHandlers
{
    public class AddClassHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.AddClass;
        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            ClassModel cls = JsonMapper.ToObject<ClassModel>(resquestPacket.DataStr);
            if (ClassDAL.CheckExist(cls.Name))
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "已存在此班级:" + cls.Name;
                client.Send(ResponsePacket);
                return;
            }
            ResponsePacket.ReturnCode = ReturnCode.Success;
            cls.Id = ClassDAL.AddClass(cls);
            ResponsePacket.DataObject = cls;
            client.Send(ResponsePacket);
        }
    }

    public class DeleteClassHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.DeleteClass;

        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            //读取数据库
            List<int> list = JsonMapper.ToObject<List<int>>(resquestPacket.DataStr);
            List<int> bakc = list.Where(i =>
                ClassDAL.Delete(i)
            ).ToList();
            ResponsePacket.DataObject = bakc;
            client.Send(ResponsePacket);
        }
    }

    public class ModifyClassHandler : HandlerBase<OESPClientChannel>
    {
        public override PacketKey PKey => PacketKey.ModifyClass;

        public override void Execute(OESPClientChannel client, Request resquestPacket)
        {
            base.Execute(client, resquestPacket);
            ClassModel cm = JsonMapper.ToObject<ClassModel>(resquestPacket.DataStr);

            if (ClassDAL.Modify(cm))
            {
                ResponsePacket.DataObject = cm;
                ResponsePacket.ReturnCode = ReturnCode.Success;
            }
            else
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "修改失败，已存在此班级！";
            }


            client.Send(ResponsePacket);
        }
    }
}